package com.example.houtai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.houtai.entity.SysUser;
import com.example.houtai.entity.TbSmsRecords;
import com.example.houtai.service.ISysUserService;
import com.example.houtai.service.ITbSmsRecordsService;
import com.example.houtai.util.SMSUtil;
import com.org.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/sys-login")
public class LoginController {


    /**
     * appId   请联系销售，或者在页面中 获取
     */
    @Value("${SMS.appId}")
    String appId;

    // 密钥 请联系销售，或者在页面中 获取
    @Value("${SMS.secretKey}")
    String secretKey;

    // 接口地址 请联系销售获取
    @Value("${SMS.host}")
    String host;

    // 加密算法
    @Value("${SMS.algorithm}")
    String algorithm;

    // 编码
    String encode = "UTF-8";

    // 是否压缩
    boolean isGizp = true;

    @Autowired
    private ITbSmsRecordsService smsRecordsService;

    @Autowired
    private ISysUserService sysUserService;


    @Resource
    PasswordEncoder passwordEncoder;

    @RequestMapping("/getCode")
    public ResultEntity getCode(TbSmsRecords smsRecords){
        QueryWrapper<TbSmsRecords> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",smsRecords.getMobile());
        List<TbSmsRecords> list = smsRecordsService.list(wrapper);
        if(list.size()>0){
            TbSmsRecords records = list.get(list.size()-1);
            Date nowDate = new Date();
            if((nowDate.getTime() - records.getCreateTime().getTime()) / 1000 < 120){
                return ResultEntity.error("请勿2分钟内重复发送短信",false);
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i<6;i++){
            sb.append((int)(Math.random()*10));
        }
        log.info("验证码："+sb.toString());
        SMSUtil.smsSend(appId,secretKey,host,algorithm,encode,isGizp,sb.toString(),"2",smsRecords.getMobile());
        smsRecords.setMobileCode(sb.toString());
        smsRecords.setCreateTime(new Date());
        smsRecords.setParam("{\"code\":\""+sb.toString()+"\"}");
        smsRecords.setStatus("101");
        smsRecordsService.save(smsRecords);
        return ResultEntity.ok(smsRecords);
    }
    @RequestMapping("reg")
    public ResultEntity reg(@RequestBody SysUser sysUser, HttpServletResponse response){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",sysUser.getMobile());
        List<SysUser> list = sysUserService.list(wrapper);
        if(list.size()>0){
            return ResultEntity.error("用户已存在!",false);
        }
        QueryWrapper<TbSmsRecords> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("mobile",sysUser.getMobile());
        List<TbSmsRecords> list1 = smsRecordsService.list(wrapper1);
        if(list1.size()>0){
            TbSmsRecords records = list1.get(list1.size()-1);
            Date nowDate = new Date();
            if((nowDate.getTime() - records.getCreateTime().getTime()) / 1000 > 120){
                return ResultEntity.error("验证码已过期!!",false);
            }
            QueryWrapper<TbSmsRecords> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("mobile",sysUser.getMobile());
            wrapper2.eq("mobile_code",sysUser.getCode());
            List<TbSmsRecords> list2 = smsRecordsService.list(wrapper2);
            if(list2.size()<1){
                return ResultEntity.error("验证码错误!!",false);
            }
        }
        sysUser.setCreateTime(new Date());
        String encode = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);

        sysUser.setContactMember(sysUser.getUsername());
        sysUser.setUsername(sysUser.getMobile());
        sysUser.setDelFlag(0);
        sysUser.setStatus(1);
        sysUser.setApprovalFlag(4);
        sysUserService.save(sysUser);
        return ResultEntity.ok(sysUser.getUserId());
    }
}
